//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;
using CommunityServer.Discussions.Controls;

namespace CommunityServer.ControlPanel.Moderation
{
	/// <summary>
	/// Summary description for Tags.
	/// </summary>
	public class Tags : BaseModerationPage
	{
		protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		protected CommunityServer.ControlPanel.Controls.PanelTabControl PanelTabControl1;
		protected Repeater theTags;
		protected IPagedControl pager;
        
		private void Page_Load(object sender, System.EventArgs e)
		{
			theTags.ItemCreated +=new RepeaterItemEventHandler(theTags_ItemCreated);
			theTags.ItemCommand +=new RepeaterCommandEventHandler(theTags_ItemCommand);
			JavaScript.RegisterRefresh(this);
			if(!IsPostBack)
			{
				ArrayList al = CommunityServer.Components.Tags.GetTags(ApplicationType.Forum, false);
				pager.TotalRecords = al.Count;
				pager.PageSize = Globals.SafeInt(this.CurrentUser.GetExtendedAttribute("CPPageSize"),10);

				int start = pager.PageIndex * pager.PageSize;
				int length = pager.PageSize;
				if (al.Count > 0)
				{
					if (start >= al.Count)
					{
						pager.PageIndex = 0;
						start = 0;
					}

					al = al.GetRange(start, start + length >= al.Count ? al.Count - start : length);
				}

				theTags.DataSource = al;
				theTags.DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void theTags_ItemCreated(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				LinkButton lb = e.Item.FindControl("DeleteButton") as LinkButton;
				if(lb != null)
				{
					lb.Attributes.Add("onclick", "return confirm('" + CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Moderation_Tags_Delete_Warning") + "');");
				}

				LiteralOrLink lorl = e.Item.FindControl("PostsLink") as LiteralOrLink;
				if(lorl != null)
				{
					Tag t = e.Item.DataItem as Tag;
					lorl.TextOnly = t.TotalCount <= 0;
				}
			}
		}

		private void theTags_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if(e.CommandName == "Delete")
			{
				string tag = e.CommandArgument.ToString();
				if(!Globals.IsNullorEmpty(tag))
				{
					foreach (PostCategory pc in PostCategories.GetCategories(tag, ApplicationType.Forum))
						PostCategories.DeleteCategory(pc.CategoryID, pc.SectionID);
				}
			}

			Response.Redirect(Request.Url.ToString());
			Response.End();
		}
	}
}
